package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import com.wan37.protobuf.message.client2server.game.GameServerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-07 14:48
 **/
@Component(PtoNum.AOIROLEBROADCASTRESPONSE + "")
public class AoiRoleBroadcastResponse implements ReceiveMsg {
    private final static Logger logger = LoggerFactory.getLogger(AoiBroadcastResponse.class);
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Autowired
    private UserServiceCache userServiceCache;

    @Override
    public void execute(Message msg) {
        GameServerResponse.AoiRoleBroadcastResponse aoiRoleBroadcastResponse = (GameServerResponse.AoiRoleBroadcastResponse) msg;
        int code = aoiRoleBroadcastResponse.getCode();
        String userId = "";
        List<GameEntity.Role> roleList = aoiRoleBroadcastResponse.getRoleList();
        if (roleList != null && !roleList.isEmpty()) {
            userId = roleList.get(0).getUserId();
        }
        switch (code) {
            case Common.SERVER_AUTO_HP_REVERT:
                clientHandler.updateAOIRole(aoiRoleBroadcastResponse.getRoleList());
                logger.info(aoiRoleBroadcastResponse.getSenderId() + " 用户实体Hp更新成功！");
                break;
            case Common.GAME_ROLE_SUCCESS:
                /**
                 * 不存在说明是有用户初次进入场景，需要初始化
                 */
                if (!userServiceCache.getUserInMap().containsKey(userId)) {
                    List<String> roleIdList = new ArrayList<>();
                    UserServiceCache.UserIn userIn = userServiceCache.new UserIn();
                    for (GameEntity.Role role : roleList) {
                        roleIdList.add(role.getAllRole().getAllRoleId());
                    }
                    userIn.setRoleIdList(roleIdList);
                    userServiceCache.getUserInMap().put(userId, userIn);
                }else {
                    List<String> roleIdList = new ArrayList<>();
                    for (GameEntity.Role role : roleList) {
                        roleIdList.add(role.getAllRole().getAllRoleId());
                    }
                    userServiceCache.getUserInMap().get(userId).setRoleIdList(roleIdList);
                }
                clientHandler.updateAOIRole(aoiRoleBroadcastResponse.getRoleList());
                logger.info("有用户实体信息更新！");
                break;
            case Common.IRONY_USE:
                clientHandler.updateAOIRole(aoiRoleBroadcastResponse.getRoleList());
                logger.info(aoiRoleBroadcastResponse.getSenderId() + " 嘲讽技能的使用！");
                break;
            case Common.IRONY_CANCEL:
                clientHandler.updateAOIRole(aoiRoleBroadcastResponse.getRoleList());
                logger.info(aoiRoleBroadcastResponse.getSenderId() + " 嘲讽技能的取消！");
                break;
            default:
                break;
        }
    }
}
